using System;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace chemmind
{
    public partial class ChartInfo : Form
    {
        internal Chart Chart;
        // 初始点位 （Mouse Down）
        private int _draggedIndex;
        // 初始数据
        private DataPoint _draggedDataPoint;
        // 松开点位 （Mouse Up）
        private int _targetIndex;
        // 松开数据
        private DataPoint _targetDataPoint;
        // 通知主窗体数据发生变化
        public event EventHandler<DataChangedEventArgs> DataChanged;

        public ChartInfo(Chart chart)
        {
            InitializeComponent();
            InitializeChart(chart);
        }

        private void InitializeChart(Chart chart)
        {
            // 创建一个新的 Chart 控件并设置其属性
            Chart = new Chart();
            Chart.Dock = DockStyle.Fill;
            ChartArea chartArea = new ChartArea();
            Legend legend = new Legend();

            Chart.ChartAreas.Add(chartArea);
            Chart.Legends.Add(legend);

            // 复制每个系列的数据
            foreach (Series series in chart.Series)
            {
                Series newSeries = new Series(series.Name)
                {
                    ChartType = series.ChartType,
                    Color = series.Color,
                    BorderWidth = series.BorderWidth
                };

                // 添加每个点的数据
                foreach (var point in series.Points)
                {
                    newSeries.Points.AddXY(point.XValue, point.YValues.First());
                }
                for (var i = 0; i < series.Points.Count; i++)
                {
                    var newSeriesPoint = newSeries.Points[i];
                    newSeriesPoint.YValues = series.Points[i].YValues;
                    newSeriesPoint.XValue = series.Points[i].XValue;
                    newSeriesPoint.Color = series.Points[i].Color;
                }

                Chart.Series.Add(newSeries);
            }

            // 复制图表标题
            foreach (Title title in chart.Titles)
            {
                Title newTitle = new Title(title.Text)
                {
                    Font = title.Font,
                    ForeColor = title.ForeColor
                };
                Chart.Titles.Add(newTitle);
            }

            Chart.Name = chart.Name;
            Controls.Add(Chart);
            Chart.MouseDown += chart_MouseDown;
            Chart.MouseUp += chart_MouseUp;
        }

        private void chart_MouseDown(object sender, MouseEventArgs e)
        {
            // 获取当前点击的图表数据点
            HitTestResult hit = Chart.HitTest(e.X, e.Y);
            if (hit.Series != null && hit.PointIndex >= 0)
            {
                _draggedIndex = hit.PointIndex;
                _draggedDataPoint = hit.Series.Points[hit.PointIndex];
            }
        }

        private void chart_MouseUp(object sender, MouseEventArgs e)
        {
            if (_draggedDataPoint != null)
            {
                HitTestResult hit = Chart.HitTest(e.X, e.Y);
                if (hit.Series != null && hit.PointIndex >= 0 && hit.PointIndex !=  _draggedIndex)
                {
                    _targetIndex = hit.PointIndex;
                    _targetDataPoint = hit.Series.Points[hit.PointIndex];
                    // 交换 Y 值
                    (_draggedDataPoint.YValues[0], _targetDataPoint.YValues[0]) =
                        (_targetDataPoint.YValues[0], _draggedDataPoint.YValues[0]);
                    // 交换 X 值
                     (_draggedDataPoint.XValue, _targetDataPoint.XValue) = (_targetDataPoint.XValue, _draggedDataPoint.XValue);
                    // 刷新图表以显示更改
                    _draggedDataPoint.Color = System.Drawing.Color.Red;
                    _targetDataPoint.Color = System.Drawing.Color.Red;
                    Chart.Invalidate();
                    
                    // 触发事件
                    OnDataChanged(new DataChangedEventArgs
                    {
                        DraggedPoint = new DataPointInfo
                        {
                            Index = _draggedIndex,
                            XValue = _draggedDataPoint.XValue,
                            YValue = _draggedDataPoint.YValues.First(),
                            Color = _draggedDataPoint.Color
                        },
                        TargetPoint = new DataPointInfo
                        {
                            Index = _targetIndex,
                            XValue = _targetDataPoint.XValue,
                            YValue = _targetDataPoint.YValues.First(),
                            Color = _targetDataPoint.Color
                        }
                    });
                }

                // 清除拖动数据点
                _draggedDataPoint = null;
                _draggedIndex = -1;
                _targetDataPoint = null;
                _targetIndex = -1;
            }
        }
        
        protected virtual void OnDataChanged(DataChangedEventArgs e)
        {
            DataChanged?.Invoke(this, e);
        }
    }
    
    // 事件参数
    public class DataChangedEventArgs : EventArgs
    {
        public DataPointInfo DraggedPoint { get; set; }
        public DataPointInfo TargetPoint { get; set; }
    }

    // 数据点信息类
    public class DataPointInfo
    {
        public int Index { get; set; }
        public double XValue { get; set; }
        public double YValue { get; set; }
        public System.Drawing.Color Color { get; set; }
    }
}